package dynamic.mybatisplus.core.injector.methods;

import com.baomidou.mybatisplus.core.enums.SqlMethod;
import dynamic.DynamicEntity;
import dynamic.ibatis.mapping.DMappedStatement;
import dynamic.ibatis.mapping.DSqlSource;
import dynamic.metadata.DTableInfo;
import dynamic.metadata.EntityClass;
import dynamic.mybatisplus.core.injector.DAbstractMethod;

/**
 * 查询满足条件总记录数
 *
 * @author hubin
 * @since 2018-04-08
 */
public class SelectCount extends DAbstractMethod {

    public SelectCount() {
        this(SqlMethod.SELECT_COUNT.getMethod());
    }

    /**
     * @param name 方法名
     * @since 3.5.0
     */
    public SelectCount(String name) {
        super(name);
    }

    @Override
    public DMappedStatement injectMappedStatement(Class<?> mapperClass, EntityClass modelClass, DTableInfo tableInfo) {
        SqlMethod sqlMethod = SqlMethod.SELECT_COUNT;
        String sql = String.format(sqlMethod.getSql(), sqlFirst(), sqlCount(), tableInfo.getTableName(),
                sqlWhereEntityWrapper(true, tableInfo), sqlComment());
        DSqlSource sqlSource = super.createSqlSource(configuration, sql, DynamicEntity.class);
        return this.addSelectMappedStatementForOther(mapperClass, methodName, sqlSource, Long.class);
    }
}
